// AWTListPeer.cpp : implementation file
//

#include "stdafx.h"
#include "awt.h"
#include "AWTListPeer.h"
#include "AWTList.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

IMPLEMENT_DYNAMIC(AWTListPeer, CListBox)

/////////////////////////////////////////////////////////////////////////////
// AWTListPeer

AWTListPeer::AWTListPeer(AWTComponent* comp, BOOL bMulti)
{
	m_comp = comp;
	m_bMulti = bMulti;
	m_AWTWindow = NULL;
	m_sel = NULL;
	m_bCapture = false;
}

AWTListPeer::~AWTListPeer()
{
	if (m_hWnd != NULL)
		DestroyWindow();
	if (m_sel != NULL)
		delete m_sel;
}

BEGIN_MESSAGE_MAP(AWTListPeer, CListBox)
	//{{AFX_MSG_MAP(AWTListPeer)
	ON_WM_KEYDOWN()
	ON_WM_CHAR()
	ON_WM_KILLFOCUS()
	ON_WM_SETFOCUS()
	ON_WM_KEYUP()
	ON_WM_CREATE()
	ON_CONTROL_REFLECT(LBN_DBLCLK, OnDblclk)
	ON_WM_DESTROY()
	ON_WM_CTLCOLOR_REFLECT()
	ON_CONTROL_REFLECT(LBN_SELCHANGE, OnSelchange)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void AWTListPeer::addItem(CString& item, int index) 
{
	InsertString(index, item);
}

void AWTListPeer::replaceItem(CString& newValue, int index) 
{
	InsertString(index, newValue);
	DeleteString(index+1);
}

void AWTListPeer::clear() 
{
	ResetContent();
}

void AWTListPeer::select(int index) 
{
	if (m_bMulti)
		SetSel(index, TRUE);
	else
	{
		SetCurSel(index);
		OnSelchange();
	}
}

void AWTListPeer::deselect(int index)
{
	if (m_bMulti)
		SetSel(index, FALSE);
	else
	{
		SetCurSel(-1);
		OnSelchange();
	}
}

void AWTListPeer::setMultipleSelections(BOOL b) 
{
}

void AWTListPeer::makeVisible(int index)
{
	SetTopIndex(index);
}

void AWTListPeer::delItems(int start, int end)
{
	for (int i = end; i >= start; i--) 
	{
	    DeleteString(i);
	}
}

void AWTListPeer::getSelectedIndexes(int** indexes, int* cnt)
{
	*cnt = GetSelCount();
	if (*cnt != 0)
	{
		*indexes = new int[*cnt];
		GetSelItems(*cnt, *indexes);
	}
}

int AWTListPeer::getSelectedIndex()
{
	int n = GetCurSel();
	if (n == LB_ERR)
		return -1;
	else
		return n;
}

/////////////////////////////////////////////////////////////////////////////
// AWTListPeer message handlers

void AWTListPeer::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	time_t t=0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTKeyEvent e(m_comp, AWTKeyEvent::KEY_PRESSED, (long)t, nFlags, nChar, nChar);
	m_comp->processKeyEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTKEYEVENT, 0, (LPARAM)&e);

	if (IsWindow(m_hWnd))
		CListBox::OnKeyDown(nChar, nRepCnt, nFlags);
}

void AWTListPeer::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	time_t t=0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTKeyEvent e(m_comp, AWTKeyEvent::KEY_TYPED, (long)t, nFlags, nChar, nChar);
	m_comp->processKeyEvent(&e);
	if (nChar == VK_TAB)
		return;
	if (nChar == VK_RETURN)
		return;
	CListBox::OnChar(nChar, nRepCnt, nFlags);
}

void AWTListPeer::OnKillFocus(CWnd* pNewWnd) 
{
	CListBox::OnKillFocus(pNewWnd);
	
	AWTFocusEvent e(m_comp, AWTFocusEvent::FOCUS_LOST);
	m_comp->processFocusEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTFOCUSEVENT, 0, (LPARAM)&e);
}

void AWTListPeer::OnSetFocus(CWnd* pOldWnd) 
{
	CListBox::OnSetFocus(pOldWnd);

	AWTFocusEvent e(m_comp, AWTFocusEvent::FOCUS_GAINED);
	m_comp->processFocusEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTFOCUSEVENT, 0, (LPARAM)&e);
}

void AWTListPeer::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	time_t t=0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTKeyEvent e(m_comp, AWTKeyEvent::KEY_RELEASED, (long)t, nFlags, 0, nChar);
	m_comp->processKeyEvent(&e);
	CListBox::OnKeyUp(nChar, nRepCnt, nFlags);
}

int AWTListPeer::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CListBox::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	m_AWTWindow = getAWTWindow(this);
/*
	CWnd* wnd = this;
	CString str;
	BOOL bFound = FALSE;
	do {
		wnd = wnd->GetParent();
		wnd->GetWindowText(str);
		if (str == "!~AWTWindow~!")
		{
			bFound = TRUE;
			break;
		}
		else if (wnd->IsKindOf(RUNTIME_CLASS(AWTDialogPeer)))
		{
			bFound = TRUE;
			break;
		}
		else if (wnd->IsKindOf(RUNTIME_CLASS(AWTMdiChildWnd)))
		{
			bFound = TRUE;
			break;
		}
	} while (wnd != NULL);
	if (bFound)
		m_AWTWindow = wnd;		
*/
	return 0;
}


void AWTListPeer::OnDblclk() 
{
	CString s;
	AWTActionEvent e(m_comp, AWTActionEvent::ACTION_PERFORMED, &s);
	((AWTList*)m_comp)->processActionEvent(&e);
}

void AWTListPeer::OnDestroy() 
{
	CListBox::OnDestroy();
	AWTWindowEvent e(m_comp, AWTWindowEvent::WINDOW_CLOSED);
//	m_comp->processWindowEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTWINDOWEVENT, 0, (LPARAM)&e);
}

HBRUSH AWTListPeer::CtlColor(CDC* pDC, UINT nCtlColor) 
{
	AWTColor* fg = m_comp->getForeground();
	AWTColor* bk = m_comp->getBackground();
	CBrush* br = m_comp->getBkBrush();

	if (fg != NULL)
		pDC->SetTextColor(fg->getColorRef());
	if (bk != NULL)
		pDC->SetBkColor(bk->getColorRef());
	return (HBRUSH)*br;
}

//jd 7/12/00
void AWTListPeer::OnSelchange() 
{
	int* newSel;
	CString str;
	AWTList* pList = (AWTList*)m_comp;
	if (m_bMulti)
	{
		//This is only used for Vision (Group demo) - multi selection
		//listboxes not supported. So leave for now. - jrd 7/12/00
	}
	else
	{
		int n = GetCurSel();
		if (n == LB_ERR)
		{
			if (m_sel != NULL)
			{
				CString str;
				GetText(m_sel[0], str);
				AWTItemEvent ev(pList, m_sel[0], (DWORD)&str, AWTItemEvent::DESELECTED);
				TRACE(_T("AWTItemEvent::DESELECTED, index: %d, item: %s\n"), m_sel[0], str);
				pList->processItemEvent(&ev);
				delete m_sel;
				m_sel = NULL;
				return;
			}			
		}
		newSel = new int[1];
		newSel[0] = n;
		if (m_sel != NULL)
		{
			GetText(m_sel[0], str);
			AWTItemEvent ev(pList, m_sel[0], (DWORD)&str, AWTItemEvent::DESELECTED);
			TRACE(_T("AWTItemEvent::DESELECTED, index: %d, item: %s\n"), m_sel[0], str);
			pList->processItemEvent(&ev);
			delete m_sel;
		}			
		m_sel = newSel;
		GetText(m_sel[0], str);
		AWTItemEvent ev(pList, m_sel[0], (DWORD)&str, AWTItemEvent::SELECTED);
//		ASSERT(0);
		TRACE(_T("AWTItemEvent::SELECTED, index: %d, item: %s\n"), m_sel[0], str);
		pList->processItemEvent(&ev);
	}	
}

void AWTListPeer::OnLButtonDown(UINT nFlags, CPoint point) 
{
	time_t t = 0;
#ifndef UNDER_CE
	time(&t);
#endif

	AWTMouseEvent e(m_comp, AWTMouseEvent::MOUSE_PRESSED, (long)t, 0, 
		point.x, point.y, 1, FALSE);
	m_comp->processMouseEvent(&e);

	m_bCapture = true;
	SetCapture();

	CListBox::OnLButtonDown(nFlags, point);
}

void AWTListPeer::OnLButtonUp(UINT nFlags, CPoint point) 
{
	time_t t = 0;
#ifndef UNDER_CE
	time(&t);
#endif

	AWTMouseEvent e(m_comp, AWTMouseEvent::MOUSE_RELEASED, (long)t, 0, 
		point.x, point.y, 1, FALSE);
	m_comp->processMouseEvent(&e);
	
	if (m_bCapture)
	{
		m_bCapture = false;
		ReleaseCapture();
		CRect rc;
		GetClientRect(&rc);
		if (rc.PtInRect(point))
		{
			AWTMouseEvent e2(m_comp, AWTMouseEvent::MOUSE_CLICKED, (long)t, 0, 
				point.x, point.y, 1, FALSE);
			m_comp->processMouseEvent(&e2);
		}
	}

	CListBox::OnLButtonUp(nFlags, point);
}
